package com.custom.base.common.view

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.custom.base.R
import com.custom.util.setVisible
import kotlinx.android.synthetic.main.title_bar_layout.view.*

class TitleBar @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    FrameLayout(context, attrs, defStyleAttr) {
    //是否显示右侧第一个图标
    private var showRightOne = true
    private var rightOneSrc: Drawable? = null

    //是否显示右侧第二个图标
    private var showRightTwo = true
    private var rightTwoSrc: Drawable? = null

    //是否显示左侧第一个图标
    private var showLeftImg = true
    private var leftOneSrc: Drawable? = null

    //是否显示左侧第一个文字
    private var showLeftText = true
    private var titleLeftText: String? = null

    /**
     * 入口
     */
    init {
        //获取自定义属性
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBar)
        showRightOne = typedArray.getBoolean(R.styleable.TitleBar_showRightOne, true)
        showRightTwo = typedArray.getBoolean(R.styleable.TitleBar_showRightTwo, true)
        showLeftImg = typedArray.getBoolean(R.styleable.TitleBar_showLeftImg, true)
        showLeftText = typedArray.getBoolean(R.styleable.TitleBar_showLeftText, true)
        titleLeftText = typedArray.getString(R.styleable.TitleBar_leftText)
        rightOneSrc = typedArray.getDrawable(R.styleable.TitleBar_rightOneSrc)
        rightTwoSrc = typedArray.getDrawable(R.styleable.TitleBar_rightTwoSrc)
        leftOneSrc = typedArray.getDrawable(R.styleable.TitleBar_leftOneSrc)
        //初始化视图
        initView()
        //        回收
        typedArray.recycle()
    }

    public lateinit var leftImage: ImageView
    public lateinit var leftText: TextView
    public lateinit var titleRightCamera: ImageView
    public lateinit var rightOneImg: ImageView
    public lateinit var rightTwoImg: ImageView
    public lateinit var layout: LinearLayout

    /**
     * 初始化视图
     */
    private fun initView() {
        View.inflate(context, R.layout.title_bar_layout, this)

        //控件赋值
        layout = title_layout
        leftImage = title_left_image
        leftText = title_left_text
        rightOneImg = title_right_one
        rightTwoImg = title_right_two
        titleRightCamera = title_right_camera

        //设置展示情况
        title_left_image.setVisible(showLeftImg)
        title_left_text.setVisible(showLeftText)
        title_right_one.setVisible(showRightOne)
        title_right_two.setVisible(showRightTwo)

        //设置资源
        rightOneSrc?.let {
            title_right_one.setImageDrawable(it)
        }
        rightTwoSrc?.let {
            title_right_two.setImageDrawable(it)
        }
        leftOneSrc?.let {
            title_left_image.setImageDrawable(it)
        }
        titleLeftText?.let {
            title_left_text.text = it
        }

        //返回图标默认实现（关闭Activity）
        //        mLeftIv.onClick {
        //            if (context is Activity) {
        //                (context as Activity).finish()
        //            }
        //        }

    }

}
